{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4b90aa2d-bf3e-4d43-807d-87d65209d444",
   "metadata": {},
   "source": [
    "# Get Started"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b36749a3-4e3e-4fb3-9c13-f5a3a3794cd6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pylint: disable=W0104\n",
    "import sys\n",
    "sys.path.append(\"../\")\n",
    "\n",
    "import numpy as np\n",
    "import mindspore as ms\n",
    "import mindspore.numpy as msnp\n",
    "\n",
    "from quditop.gates import H, X, Y, Z, RX, RY, RZ, UMG, SWAP, GP\n",
    "from quditop.circuit import Circuit\n",
    "\n",
    "ms.set_context(mode=ms.PYNATIVE_MODE)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48142d0e-9506-4a38-8033-c2c6583f296c",
   "metadata": {},
   "source": [
    "## Example: SWAP gate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ca185dbc-410f-43a4-835f-ae72d185b453",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Swap the q1-q2 when q0=1:\n",
      "1¦0120⟩\n",
      "Swap the q1-q2 when q0=1:\n",
      "1¦1210⟩\n",
      "Swap the q2-q3 when q1=2:\n",
      "1¦1201⟩\n"
     ]
    }
   ],
   "source": [
    "dim = 3\n",
    "n_qudits = 4\n",
    "\n",
    "ctrl_qudits = [0]\n",
    "ctrl_states = [1]\n",
    "cir = Circuit(dim, n_qudits, gates=[\n",
    "    X(dim, ind=[0, 2]).on(2),\n",
    "    X(dim, ind=[1, 0]).on(1),\n",
    "    SWAP(dim).on([1, 2], ctrl_qudits, ctrl_states)\n",
    "])\n",
    "\n",
    "qs1 = cir.get_qs(ket=True)\n",
    "print(f\"Swap the q1-q2 when q0=1:\\n{qs1}\")\n",
    "\n",
    "cir2 = Circuit(dim, n_qudits, gates=[\n",
    "    X(dim, ind=[0, 1]).on(0),    # add this line\n",
    "    X(dim, ind=[0, 2]).on(2),\n",
    "    X(dim, ind=[1, 0]).on(1),\n",
    "    SWAP(dim).on([1, 2], 0, 1)\n",
    "])\n",
    "\n",
    "qs2 = cir2.get_qs(ket=True)\n",
    "print(f\"Swap the q1-q2 when q0=1:\\n{qs2}\")\n",
    "\n",
    "\n",
    "cir3 = Circuit(dim, n_qudits, gates=[\n",
    "    X(dim, ind=[0, 1]).on(0),    # add this line\n",
    "    X(dim, ind=[0, 2]).on(2),\n",
    "    X(dim, ind=[1, 0]).on(1),\n",
    "    SWAP(dim).on([1, 2], 0, 1),\n",
    "    SWAP(dim).on([2, 3], 1, 2)   # SWAP q2-q3 when q1=2\n",
    "])\n",
    "\n",
    "qs3 = cir3.get_qs(ket=True)\n",
    "print(f\"Swap the q2-q3 when q1=2:\\n{qs3}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7c0da4e-92e8-417d-96ed-70df5e93e12e",
   "metadata": {},
   "source": [
    "## Circuit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e209eb92-59dc-4573-a993-55424a49f2a0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.        +0.j         0.        +0.j         0.        +0.j\n",
      " 0.        +0.j         0.        +0.62054455j 0.29750493+0.j\n",
      " 0.        +0.j         0.16252768+0.j         0.        +0.j\n",
      " 0.        +0.j         0.        +0.j         0.16252768+0.j\n",
      " 0.        +0.62054455j 0.29750493+0.j         0.        +0.j\n",
      " 0.        +0.j        ]\n"
     ]
    }
   ],
   "source": [
    "# Build a quantum circuit\n",
    "\n",
    "dim = 2\n",
    "n_qudits = 4\n",
    "\n",
    "circ = Circuit(dim, n_qudits, gates=[\n",
    "    H(dim, [0,1]).on(0),\n",
    "    X(dim, [0,1]).on(0, [2,3], [1, 1]),\n",
    "    Y(dim, [0,1]).on(1),\n",
    "    Z(dim, [0,1]).on(1, 2, 1),\n",
    "    RX(dim, [0,1], pr='param').on(3),\n",
    "    RY(dim, [0,1], pr=1.0).on(2, 3, 1),\n",
    "    X(dim, [0,1]).on(1, [0, 2, 3], 1),\n",
    "    RY(dim, [0,1], pr=2.0).on(3, [0, 1, 2], 1)\n",
    "])\n",
    "\n",
    "print(circ.get_qs())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3f58659e-1bb9-4828-8f0c-208ee4132ece",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['param', '_param_', '_param_']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# print the parameter names, where the `_param_` is default name.\n",
    "\n",
    "circ.param_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e440de16-2ef2-4949-8326-4859e30435fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Circuit<\n",
      "  (gates): CellList<\n",
      "    (0): H(2|0)\n",
      "    (1): X(2 [0 1]|0 <-: 2 3 - 1 1)\n",
      "    (2): Y(2 [0 1]|1)\n",
      "    (3): Z(2 [0 1]|1 <-: 2 - 1)\n",
      "    (4): RX(2 [0 1] param|3)\n",
      "    (5): RY(2 [0 1] _param_|2 <-: 3)\n",
      "    (6): X(2 [0 1]|1 <-: 0 2 3 - 1 1 1)\n",
      "    (7): RY(2 [0 1] _param_|3 <-: 0 1 2)\n",
      "    (8): RZ(2 [0 1] pr_z|3)\n",
      "    (9): RX(2 [0 1] _param_|2)\n",
      "    (10): X(2 [0 1]|2)\n",
      "    (11): Y(2 [0 1]|0 <-: 3 - 1)\n",
      "    (12): Z(2 [0 1]|1)\n",
      "    (13): Z(2 [0 1]|2)\n",
      "    >\n",
      "  >\n"
     ]
    }
   ],
   "source": [
    "# We can add new gate(s) or circuit to old circuit easily.\n",
    "# circ = Circuit(dim, n_qudits)\n",
    "\n",
    "circ += RZ(dim, [0,1], pr='pr_z').on(3)\n",
    "circ.append(RX(dim, [0,1], pr=2.0).on(2))\n",
    "circ += X(dim, [0,1]).on(2)\n",
    "circ += Y(dim, [0,1]).on(0, 3)\n",
    "circ += Circuit(dim, n_qudits, gates=[Z(dim, [0,1]).on(1), Z(dim, [0,1]).on(2)])\n",
    "\n",
    "print(circ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ae391004-8807-4aae-9761-701f1406d9f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==========================Circuit Summary==========================\n",
      "|Total number of gates   : 14.                                    |\n",
      "|Parameter gates         : 5.                                     |\n",
      "|with 5 parameters are   :                                        |\n",
      "|param, _param_, _param_, pr_z, _param_                         . |\n",
      "|Number qudits of circuit: 4                                      |\n",
      "===================================================================\n"
     ]
    }
   ],
   "source": [
    "# print the summary of circuit.\n",
    "\n",
    "circ.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "14f172ea-b809-490a-b74e-82e82aecfa89",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "quantum state in numpy.array:\n",
      "[ 0.        +0.j          0.04210031-0.0770641j   0.        +0.j\n",
      "  0.12002023+0.06556736j -0.4582475 +0.25034174j  0.21969557+0.12002025j\n",
      "  0.16074258+0.29423732j  0.07706411-0.14106488j  0.        +0.j\n",
      "  0.        +0.j          0.        +0.j          0.        +0.j\n",
      " -0.4582475 +0.25034174j -0.17759526-0.19708434j  0.16074258+0.29423732j\n",
      "  0.04295612+0.20663224j]\n",
      "\n",
      "quantum state in ket:\n",
      "0.0421-0.0771j¦0001⟩\n",
      "0.12+0.0656j¦0011⟩\n",
      "-0.4582+0.2503j¦0100⟩\n",
      "0.2197+0.12j¦0101⟩\n",
      "0.1607+0.2942j¦0110⟩\n",
      "0.0771-0.1411j¦0111⟩\n",
      "-0.4582+0.2503j¦1100⟩\n",
      "-0.1776-0.1971j¦1101⟩\n",
      "0.1607+0.2942j¦1110⟩\n",
      "0.043+0.2066j¦1111⟩\n"
     ]
    }
   ],
   "source": [
    "# Get the quantum state of circuit.\n",
    "print(f\"quantum state in numpy.array:\\n{circ.get_qs()}\\n\")\n",
    "print(f\"quantum state in ket:\\n{circ.get_qs(ket=True)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8360b9d4-b95b-4491-a5aa-80f6a1de3d25",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n_qudits: 4\n",
      "qs:\n",
      "[ 0.        +0.j          0.        +0.j          0.        +0.j\n",
      "  0.        +0.j          0.        +0.j          0.        +0.j\n",
      "  0.52217025+0.33528164j  0.52217025+0.33528164j  0.        +0.j\n",
      "  0.        +0.j          0.        +0.j          0.        +0.j\n",
      "  0.        +0.j          0.        +0.j          0.2852629 -0.18316521j\n",
      " -0.2852629 +0.18316521j]\n"
     ]
    }
   ],
   "source": [
    "# We can get the state of quantum with specific value.\n",
    "default_value = 0.0\n",
    "qs = circ.get_qs({'param': 1.0, 'pr_z': 2.0, '_param_':default_value}, endian_reverse=True)\n",
    "\n",
    "print(f'n_qudits: {circ.n_qudits}')\n",
    "print(f'qs:\\n{qs}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e33281e4-cb24-4241-b4b6-1f7572fffc9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Parameter (name=4.param, shape=(), dtype=Float32, requires_grad=True),\n",
       " Parameter (name=5.param, shape=(), dtype=Float32, requires_grad=True),\n",
       " Parameter (name=7.param, shape=(), dtype=Float32, requires_grad=True),\n",
       " Parameter (name=8.param, shape=(), dtype=Float32, requires_grad=True),\n",
       " Parameter (name=9.param, shape=(), dtype=Float32, requires_grad=True)]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the parameters of circuit.\n",
    "list(circ.get_parameters())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "38415944-73e8-4307-8029-443272f15807",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Parameter (name=4.param, shape=(), dtype=Float32, requires_grad=False),\n",
       " Parameter (name=5.param, shape=(), dtype=Float32, requires_grad=False),\n",
       " Parameter (name=7.param, shape=(), dtype=Float32, requires_grad=False),\n",
       " Parameter (name=8.param, shape=(), dtype=Float32, requires_grad=False),\n",
       " Parameter (name=9.param, shape=(), dtype=Float32, requires_grad=False)]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Convert the circuit to encoder, we can see that there's no `requires_grad` for parameters.\n",
    "circ.as_encoder()\n",
    "# You can also do it by method `circ.no_grad()`\n",
    "list(circ.get_parameters())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f6614c7-e50b-4267-8c93-e2364bedd14f",
   "metadata": {},
   "source": [
    "## Compare with mindquantum when `dim = 2`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6ad839b4-1d3e-4d11-94fe-1a98764f2c06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n_qubit: 4\n",
      "mindquantum qs:\n",
      "[ 0.        +0.j          0.        +0.j         -0.        +0.j\n",
      " -0.        +0.j         -0.        +0.j         -0.        +0.j\n",
      "  0.52217026+0.33528167j  0.52217026+0.33528167j  0.        +0.j\n",
      "  0.        +0.j         -0.        +0.j         -0.        +0.j\n",
      " -0.        +0.j         -0.        +0.j          0.28526291-0.18316521j\n",
      " -0.28526291+0.18316521j]\n"
     ]
    }
   ],
   "source": [
    "# Compare with `mindquantum`\n",
    "# Here we build a circuit by mindquantum with the same parameters.\n",
    "\n",
    "import mindquantum as mq\n",
    "\n",
    "mq_circ = mq.Circuit([\n",
    "    mq.H.on(0),\n",
    "    mq.X.on(0, [2,3]),\n",
    "    mq.Y.on(1),\n",
    "    mq.Z.on(1, 2),\n",
    "    mq.RX(1.0).on(3),\n",
    "    mq.RY(0.0).on(2, 3),\n",
    "    mq.X(1, [0, 2, 3]),\n",
    "    mq.RY(0.0).on(3, [0, 1, 2]),\n",
    "    mq.RZ(2.0).on(3),\n",
    "    mq.RX(0.0).on(2),\n",
    "    mq.X(2),\n",
    "    mq.Y(0, 3),\n",
    "    mq.Z(1),\n",
    "    mq.Z(2)\n",
    "])\n",
    "\n",
    "mq_qs = mq_circ.get_qs()\n",
    "\n",
    "print(f'n_qubit: {mq_circ.n_qubits}')\n",
    "print(f'mindquantum qs:\\n{mq_qs}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4bbbef45-b64e-434a-ac51-ff4abf654b2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True]\n"
     ]
    }
   ],
   "source": [
    "# We can see that the QudiTop circuit has the same output as MindQuantum\n",
    "\n",
    "print(np.isclose(qs, mq_qs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d9899aaa-cd74-49e0-ab75-98197e775ea4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "circ2:\n",
      "Circuit<\n",
      "  (gates): CellList<\n",
      "    (0): UMG(3|2)\n",
      "    (1): UMG(3|0 <-: 2 - 1)\n",
      "    (2): RX(3 [1 2] _param_|2)\n",
      "    (3): RY(3 [0 2] _param_|3 <-: 2)\n",
      "    (4): RZ(3 [0 1] _param_|3 <-: 2)\n",
      "    (5): GP(3 gh_pr|3)\n",
      "    >\n",
      "  >\n",
      "circ2 qs:\n",
      "-0.4536+0.7065j¦0000⟩\n",
      "0.512-0.6856j¦0010⟩\n",
      "-0.415+0.0678j¦0020⟩\n",
      "-0.2167-0.0762j¦0022⟩\n",
      "0.6573-1.0236j¦1010⟩\n",
      "-0.5818-0.0413j¦1020⟩\n",
      "-0.2681-0.1721j¦1022⟩\n",
      "0.1789-0.2787j¦2010⟩\n",
      "-0.1584-0.0112j¦2020⟩\n",
      "-0.073-0.0469j¦2022⟩\n"
     ]
    }
   ],
   "source": [
    "# An exapmles of dim = 3.\n",
    "dim = 3\n",
    "n_qudits = 4\n",
    "\n",
    "circ2 = Circuit(dim, n_qudits, gates=[\n",
    "    UMG(dim, mat=msnp.randn(dim, dim)).on(2),\n",
    "    UMG(dim, mat=msnp.randn(dim, dim)).on(0, ctrl_qudits=2, ctrl_states=1),\n",
    "    RX(dim, [1, 2], pr=1.0).on(2),\n",
    "    RY(dim, [0, 2]).on(3, 2),\n",
    "    RZ(dim, [0, 1]).on(3, 2),\n",
    "    GP(dim, pr=\"gh_pr\").on(3),\n",
    "])\n",
    "\n",
    "print(f\"circ2:\\n{circ2}\")\n",
    "print(f\"circ2 qs:\\n{circ2.get_qs(ket=True)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bced9ff1-ecf0-46c2-9ede-95dc765496cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "umg.matrix:\n",
      "[[ 1.5181631 +0.j  0.04740785+0.j  0.9991546 +0.j]\n",
      " [ 0.28767666+0.j  0.28836432+0.j  1.3382761 +0.j]\n",
      " [-0.21939212+0.j  0.34292695+0.j  1.9084775 +0.j]]\n",
      "is_unitary: False\n"
     ]
    }
   ],
   "source": [
    "# Note: In QudiTop, we don't limit that the matrix must be unitary.\n",
    "# We provide the `is_unitary()` method to check if the matrix of gate is unitary.\n",
    "\n",
    "umg = UMG(dim, mat=msnp.randn(dim, dim)).on(2)\n",
    "print(f\"umg.matrix:\\n{umg.matrix()}\")\n",
    "print(f\"is_unitary: {umg.is_unitary()}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4fb920fa-85d8-47d0-a6d8-8019e7e76804",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "matrix of u:\n",
      "[[ 1.5181631 +0.j  0.04740785+0.j  0.9991546 +0.j]\n",
      " [ 0.28767666+0.j  0.28836432+0.j  1.3382761 +0.j]\n",
      " [-0.21939212+0.j  0.34292695+0.j  1.9084775 +0.j]]\n",
      "matrix of X gate:\n",
      "[[0.+0.j 0.+0.j 1.+0.j]\n",
      " [0.+0.j 1.+0.j 0.+0.j]\n",
      " [1.+0.j 0.+0.j 0.+0.j]]\n"
     ]
    }
   ],
   "source": [
    "# Get the matrix of a gate\n",
    "\n",
    "u = umg.matrix()\n",
    "print(f'matrix of u:\\n{u}')\n",
    "\n",
    "# For matrix of controlled gate, we don't actually give the part matrix of controlled gate.\n",
    "x = X(dim=3, ind=[0, 2], obj_qudits=0, ctrl_qudits=1, ctrl_states=2)\n",
    "print(f'matrix of X gate:\\n{x.matrix()}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d2e4d87f-7141-4660-803a-bf99e946b114",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Parameter (name=2.param, shape=(), dtype=Float32, requires_grad=True),\n",
       " Parameter (name=3.param, shape=(), dtype=Float32, requires_grad=True),\n",
       " Parameter (name=4.param, shape=(), dtype=Float32, requires_grad=True),\n",
       " Parameter (name=5.param, shape=(), dtype=Float32, requires_grad=True)]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the parameters\n",
    "\n",
    "list(circ2.get_parameters())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef705b20-718f-4d54-9f7d-5d57141486b5",
   "metadata": {},
   "source": [
    "## Print the matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "187c2a42-5ddd-439f-8988-2125ba7a7692",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H(3|0)\n",
      "[[ 0.57735026+0.j          0.57735026+0.j          0.57735026+0.j        ]\n",
      " [ 0.57735026+0.j         -0.2886752 +0.5j        -0.2886751 -0.50000006j]\n",
      " [ 0.57735026+0.j         -0.2886751 -0.50000006j -0.28867528+0.49999994j]]\n",
      "X(3 [0 1]|0)\n",
      "[[0.+0.j 1.+0.j 0.+0.j]\n",
      " [1.+0.j 0.+0.j 0.+0.j]\n",
      " [0.+0.j 0.+0.j 1.+0.j]]\n",
      "Y(3 [0 2]|0)\n",
      "[[0.+0.j 0.+0.j 0.-1.j]\n",
      " [0.+0.j 1.+0.j 0.+0.j]\n",
      " [0.+1.j 0.+0.j 0.+0.j]]\n",
      "Z(3 [1 2]|0)\n",
      "[[ 1.+0.j  0.+0.j  0.+0.j]\n",
      " [ 0.+0.j  1.+0.j  0.+0.j]\n",
      " [ 0.+0.j  0.+0.j -1.+0.j]]\n",
      "RX(3 [0 1] _param_|0)\n",
      "[[0.70710677+0.j        0.        -0.7071068j 0.        +0.j       ]\n",
      " [0.        -0.7071068j 0.70710677+0.j        0.        +0.j       ]\n",
      " [0.        +0.j        0.        +0.j        1.        +0.j       ]]\n",
      "RY(3 [0 2] _param_|0)\n",
      "[[ 0.70710677+0.j  0.        +0.j -0.7071068 +0.j]\n",
      " [ 0.        +0.j  1.        +0.j  0.        +0.j]\n",
      " [ 0.7071068 +0.j  0.        +0.j  0.70710677+0.j]]\n",
      "RZ(3 [1 2] _param_|0)\n",
      "[[1.        +0.j        0.        +0.j        0.        +0.j       ]\n",
      " [0.        +0.j        0.70710677-0.7071068j 0.        +0.j       ]\n",
      " [0.        +0.j        0.        +0.j        0.70710677+0.7071068j]]\n",
      "SWAP(3|0 1)\n",
      "[[1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]\n",
      " [0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]\n",
      " [0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j]\n",
      " [0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]\n",
      " [0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]\n",
      " [0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j]\n",
      " [0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]\n",
      " [0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j]\n",
      " [0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j]]\n",
      "GP(3 _param_|2)\n",
      "[[0.49999997-0.86602545j 0.        +0.j         0.        +0.j        ]\n",
      " [0.        +0.j         0.49999997-0.86602545j 0.        +0.j        ]\n",
      " [0.        +0.j         0.        +0.j         0.49999997-0.86602545j]]\n"
     ]
    }
   ],
   "source": [
    "# Print the matrix of gate\n",
    "\n",
    "d = 3\n",
    "\n",
    "h = H(d, obj_qudits=0)\n",
    "x = X(d, [0, 1], obj_qudits=0)\n",
    "y = Y(d, [0, 2], obj_qudits=0)\n",
    "z = Z(d, [1, 2], obj_qudits=0)\n",
    "rx = RX(d, [0, 1], pr=msnp.pi/2, obj_qudits=0)\n",
    "ry = RY(d, [0, 2], pr=msnp.pi/2, obj_qudits=0)\n",
    "rz = RZ(d, [1, 2], pr=msnp.pi/2, obj_qudits=0)\n",
    "swap = SWAP(d, obj_qudits=[0, 1])\n",
    "gp = GP(d, pr=msnp.pi/3, obj_qudits=2)\n",
    "\n",
    "gates = [h, x, y, z, rx, ry, rz, swap, gp]\n",
    "\n",
    "for gate in gates:\n",
    "    print(gate)\n",
    "    print(gate.matrix())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
